//
//  ChangeChipListDetailViewController.m
//  CarPark
//
//  Created by oceans on 2019/8/15.
//  Copyright © 2019 oceans. All rights reserved.
//

#import "ChangeChipListDetailViewController.h"
#import <MJRefresh.h>
#import "LowListCollectionViewCell.h"
#import "ChipChangeViewController.h"

@interface ChangeChipListDetailViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) UICollectionView *collection;
@property (strong, nonatomic) NSMutableArray *array;
@property (strong, nonatomic) MetaModel *meta;
@end

@implementation ChangeChipListDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"低电列表";
    self.array = [NSMutableArray array];
    [self loadData];
}

- (void)loadData {
    
    defweakself
    [HttpRequest getURL:ktask_detail paras:@{@"type": @"task_chip_change", @"id": [NSString stringWithFormat:@"%d", __id]} showProgress:NO successful:^(id  _Nullable result) {
        NSLog(@"%@", result);
        [weakself.array addObjectsFromArray:result[@"datas"][@"data"]];
        [weakself setupView];
        MetaModel *meta = [[MetaModel alloc] initWithDic:result[@"datas"][@"meta"][@"pagination"]];
        if (meta.current_page == meta.total_pages || meta.total == 0) {
            [weakself.collection.mj_footer endRefreshingWithNoMoreData
             ];
        }else {
            weakself.meta = meta;
        }
        [weakself.collection reloadData];
    } failed:^(int code, NSString * _Nullable errMsg) {
        
    }];
}

- (void)loadMoreData {
    
    defweakself
    [HttpRequest getURL:ktask_detail paras:@{@"type": @"task_chip_change", @"id": [NSString stringWithFormat:@"%d", __id], @"page": [NSString stringWithFormat:@"%d", _meta.current_page+1]} showProgress:NO successful:^(id  _Nullable result) {
        NSLog(@"%@", result);
        [weakself.array addObjectsFromArray:result[@"datas"][@"data"]];
        MetaModel *meta = [[MetaModel alloc] initWithDic:result[@"datas"][@"meta"][@"pagination"]];
        weakself.meta = meta;
        if (meta.current_page == meta.total_pages || meta.total == 0) {
            [weakself.collection.mj_footer endRefreshingWithNoMoreData
             ];
        }else {
            [weakself.collection.mj_footer endRefreshing];
        }
        [weakself.collection reloadData];
    } failed:^(int code, NSString * _Nullable errMsg) {
        [weakself.collection.mj_footer endRefreshing];
    }];
}

- (void)setupView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, XNavigationHeight, ScreenWidth, ScreenHeight-XNavigationHeight-XBotSafeSpace-40.0) collectionViewLayout:layout];
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collection];
    [_collection registerNib:[UINib nibWithNibName:@"LowListCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"LowListCollectionViewCell"];
    _collection.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    
    return 0.0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 0.0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(ScreenWidth, 100.0);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *dic = _array[indexPath.item];
    LowListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LowListCollectionViewCell" forIndexPath:indexPath];
    cell.label1.text = dic[@"autocar"][@"car_model"];
    cell.label2.text = dic[@"autocar"][@"color"];
    cell.label3.text = dic[@"autocar"][@"displacement"];
    cell.label4.text = [NSString stringWithFormat:@"VIN %@", dic[@"autocar"][@"vin_no"]];
    cell.label5.text = [NSString stringWithFormat:@"芯片序列号：%@", dic[@"rfid_id"]];
    cell.label6.text = [NSString stringWithFormat:@" %@ ", dic[@"power_status_text"]];
    cell.label6.backgroundColor = COLOR(253, 152, 39, 1);
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ChipChangeViewController *con = [[ChipChangeViewController alloc] init];
    con.info = _array[indexPath.item];
    [self.navigationController pushViewController:con animated:YES];
}

@end
